﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using VietPay.KooBoo.Business.Interfaces;
using VietPay.KooBoo.Business;
using VietPay.KooBoo.Business.Models;

namespace Kooboo.CMS.Modules.Areas.SalesModule.Controllers
{
    public class BatchController : Controller
    {
        private IBatchService _batchService;
        private ICategoryService _categoryService;
        private IDistributorService _distributorService;
        private IProductService _productService;

        public BatchController()
            : this(new BatchService(),new CategoryService(),new DistributorService(), new ProductService())
        {

        }

        public BatchController(IBatchService batchService, ICategoryService categoryService
            , IDistributorService distributorService, IProductService productService)
        {
            _batchService = batchService;
            _categoryService=categoryService;
            _distributorService = distributorService;
            _productService = productService;
        }

        //
        // GET: /SalesModule/News/

        #region //private
        private List<SelectListItem> _Status = new List<SelectListItem>();
        public List<SelectListItem> Status
        {
            get
            {
                _Status.Add(new SelectListItem() { Text = "Đang phân phối", Value = "1" });
                _Status.Add(new SelectListItem() { Text = "Hoàn thành", Value = "2" });
                return _Status;
            }
        }

        #endregion
        public ActionResult Index(int distributorID, string dtpFromCreateDate, string dtpToCreateDate, string batchCode, string CategoryName, string dpStatus)
        {
            //distributorID = 2;
            BatchSearchViewModel batchViewModel = new BatchSearchViewModel();
            var nlst = _batchService.GetBatchSearch(null, distributorID, batchViewModel, 10, 1);
            if (nlst == null)
            {
                return HttpNotFound();
            }

            ViewData["StatusListValue"] = new SelectList(Status, "Value", "Text");
            return View(nlst);
        }

        //
        // GET: /SalesModule/News/Details/5

        public ActionResult Details(int id=-1)
        {
            var batchViewModel = _batchService.Query(p => p.BatchID == id).FirstOrDefault();
            if (batchViewModel == null)
            {
                return HttpNotFound();
            }
            return View(batchViewModel);
        }

        //
        // GET: /SalesModule/News/SearchBath
        public ActionResult SearchBath(int distributorID, int rating)
        {
            BatchSearchViewModel batchViewModel = new BatchSearchViewModel();

            var batchListView = _batchService.GetBatchSearch(3,distributorID, batchViewModel, 10, 1);
            return View("Index", batchListView);
        }

        #region //BatchVMSCreateGood
        //
        // GET: /SalesModule/Batch/BatchVMSCreateGood
        public ActionResult BatchVMSCreateGood()
        {
            BatchListViewModel batchListViewModel = new BatchListViewModel();
            batchListViewModel.DistributorListView = _distributorService.GetDistributorList(100, 1);

            //var batchViewModel = new BatchViewModel();

            return View(batchListViewModel);
        }

        //
        // POST: /SalesModule/Batch/BatchVMSCreateGood

        [HttpPost]
        public ActionResult BatchVMSCreateGood(BatchViewModel batchViewModel)
        {
            try
            {
                // TODO: Add insert logic here
                _batchService.Add(batchViewModel);
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
        #endregion

        //
        // GET: /SalesModule/News/Create

        public ActionResult Create()
        {
            var batchViewModel = new BatchViewModel();
            return View(batchViewModel);
        } 

        //
        // POST: /SalesModule/News/Create

        [HttpPost]
        public ActionResult Create(BatchViewModel batchViewModel)
        {
            try
            {
                // TODO: Add insert logic here
                _batchService.Add(batchViewModel);
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
        
        //
        // GET: /SalesModule/News/Edit/5
 
        public ActionResult Edit(int id=-1)
        {
            var batchViewModel = _batchService.Query(p => p.BatchID == id).FirstOrDefault();
            if (batchViewModel == null)
            {
                return HttpNotFound();
            }
            return View(batchViewModel);
        }

        //
        // POST: /SalesModule/News/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, BatchViewModel batchViewModel)
        {
            try
            {
                // TODO: Add update logic here
                batchViewModel.BatchID = id;
                _batchService.Update(batchViewModel);
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /SalesModule/News/Delete/5
 
        public ActionResult Delete(int id=-1)
        {
            var batchViewModel = _batchService.Query(p => p.BatchID == id).FirstOrDefault();
            if (batchViewModel == null)
            {
                return HttpNotFound();
            }
            return View(batchViewModel);
        }

        //
        // POST: /SalesModule/News/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, BatchViewModel batchViewModel)
        {
            try
            {
                // TODO: Add delete logic here
                batchViewModel.BatchID = id;
                _batchService.Delete(batchViewModel);
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        public JsonResult JSONProduct(string q = "")
        {
            //MSTUserService service = SingletonProvider<MSTUserService>.GetInstance();
            //service.AddServiceInfo(Session["SessionId"].ToString());
            //IList<MSTUserEntity> lstUser = service.GetActiveUser(q);

            ProductListViewModel lstProduct= _productService.GetProductList(100, 1);
            return Json(lstProduct, JsonRequestBehavior.AllowGet);
        }
    }
}
